//=========================================================
// Interactive Digital Media Diploma
// CUFDIG404A_R1: Introduction to Scripting
// Assignment 3
// Author: Ben
// File: objectPool.js
// Description: a pool class for multiple similar objects such as missiles
//=========================================================

function AmmoPool(count, type, sprite){
    
    // set up the pool
    var pool = [];
    var poolItem;
    for (var idx = 0; idx < count; idx++){
        poolItem = new type(sprite);
        poolItem.setEnabled(false);
        pool.push(poolItem);
    }
    
    
    // update all of the pool items
    this.update = function(){
        var poolItem;
        for(var idx = 0; idx < count; idx++){
            poolItem = pool[idx];
            if (poolItem.isEnabled()){
                pool[idx].update();
            }
        }
    };

    
    // fire an item in the pool
    this.fire = function(x, y, x_vel, y_vel){
        for(var idx = 0; idx < count; idx++){
            poolItem = pool[idx];
            if (!poolItem.isEnabled()){
                poolItem.setEnabled(true);
                poolItem.start(x, y, x_vel, y_vel);
                break;
            }
        }
    };
}